<?php

namespace app\admin\controller\user;

use app\admin\controller\Controllers;
use app\admin\service\user\User as UserService;
use app\admin\model\user\User as UserModels;

class User extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index( UserService $service )
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\user\User.index');
        $list = $service->indexPaginate($get);
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total()
        ]);
        return $this->fetch('user/user/index');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(UserModels $model)
    {
        $this->assign([
            'data' => $model
        ]);
        return $this->fetch('user/user/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  UserModels $model
     * @param  UserService $service
     * @return \think\Response
     */
    public function update(UserModels $model, UserService $service)
    {
        $put = $this->request->only(['password'], 'put');
        $this->validate($put, 'app\admin\validate\user\User.Update');
        $update = $service->save($model, $put);
        if ( false === $update ) {
            return failed('编辑失败');
        }
        return success();
    }

}
